package com.jingdianjichi.subject.application.controller;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.domain.service.impl.SubjectLabelDomainServiceImpl;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("subject/label")
@Slf4j
public class SubjectLabelController{
    @Resource
    private SubjectLabelDomainServiceImpl subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto label", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"分类名称不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId());
            SubjectLabelBO subjectLabelBO=SubjectLabelDTOConverter.INSTANCE.convertDtoToBoLabel(subjectLabelDTO);
            subjectLabelDomainService.insert(subjectLabelBO);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail("新增标签失败");
        }

    }
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("query subjectdto label", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");
            List<SubjectLabelBO> BoList=subjectLabelDomainService.queryLabelByCategoryId(
                    SubjectLabelDTOConverter.INSTANCE.convertDtoToBoLabel(subjectLabelDTO));
            List<SubjectLabelDTO> DTOList=SubjectLabelDTOConverter.INSTANCE.convertBoToDtoLabel(BoList);
            return Result.ok(DTOList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("查询分类下标签失败");
        }

    }
   @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            boolean result=subjectLabelDomainService.update(SubjectLabelDTOConverter.INSTANCE.convertDtoToBoLabel(subjectLabelDTO));
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail("更新失败");
        }

    }

    @PostMapping("/delete")
    public Result<List<SubjectLabelDTO>> delete (@RequestBody SubjectLabelDTO  subjectLabelDTO) {
        try {
            boolean result=subjectLabelDomainService.delete(SubjectLabelDTOConverter.INSTANCE.convertDtoToBoLabel(subjectLabelDTO));
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail("删除失败");
        }

    }
}
